package com.mdd.front.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.yulichang.query.MPJQueryWrapper;
import com.mdd.common.entity.Shop;
import com.mdd.common.mapper.ShopMapper;
import com.mdd.common.util.StringUtils;
import com.mdd.front.service.IShopProductService;
import com.mdd.front.validate.shop.ShopProductSearchValidate;
import com.mdd.front.vo.shop.ShopProductDetailVo;
import com.mdd.front.vo.shop.ShopProductListedVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class ShopProductServiceImpl implements IShopProductService {
    @Resource
    ShopMapper shopMapper;


    @Override
    public List<ShopProductListedVo> list(ShopProductSearchValidate searchValidate) {
        MPJQueryWrapper<Shop> mpjQueryWrapper = new MPJQueryWrapper<>();
        /*if(!searchValidate.getName().isEmpty()){
            mpjQueryWrapper.like("name", searchValidate.getName());
        }*/
        if(null != searchValidate && StringUtils.isNotEmpty(searchValidate.getName())) {
            mpjQueryWrapper.like("name", searchValidate.getName());
        }
        mpjQueryWrapper.eq("t.status", 1);
        /*mpjQueryWrapper.select("t.id,name,image,status,sales_num+virtual_sales_num as sales_num," +
                "click_num+virtual_click_num as click_num,min_price,min_lineation_price");*/
        mpjQueryWrapper.select("t.id,t.name,t.logo,t.credit_score,t.business_license,t.fans,t.remark,t.extend,t.extend2");
        List<Shop> shopList = shopMapper.selectList(mpjQueryWrapper);
        //List<Goods> goodsList = shopMapper.selectJoinList(Goods.class, mpjQueryWrapper);
        return __copyShopsList(shopList);
    }

    /**
     * 商品列表项处理
     *
     * @author mjf
     * @param shopList List<Goods>
     * @return List<ShopProductListedVo>
     */
    private List<ShopProductListedVo> __copyShopsList(List<Shop> shopList) {
        List<ShopProductListedVo> list = new ArrayList<>();
        for (Shop shops : shopList) {
            ShopProductListedVo shopProductListedVo = new ShopProductListedVo();
            BeanUtils.copyProperties(shops, shopProductListedVo);
            shopProductListedVo.setIcon(shops.getLogo());
            list.add(shopProductListedVo);
        }
        return list;
    }

    @Override
    public List<ShopProductListedVo> all() {
        List<Shop> list = shopMapper.selectList(new LambdaQueryWrapper<Shop>());
        List<ShopProductListedVo> ret = new ArrayList<ShopProductListedVo>();
        for (Shop shop : list) {
            ShopProductListedVo vo = new ShopProductListedVo();
            BeanUtils.copyProperties(shop, vo);
            ret.add(vo);
        }
        return ret;
    }

    @Override
    public ShopProductDetailVo detail(Integer id) {
        Shop model = shopMapper.selectOne(
                new QueryWrapper<Shop>()
                        .eq("id", id)
                        .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        ShopProductDetailVo vo = new ShopProductDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

}
